import { createRouter, createWebHistory } from "vue-router";
import VueCookies from "vue-cookies";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/loading",
      name: "loading",
      component: () => import("@/components/loadingtest"),
      children: [
        {
          path: "l1",
          name: "l1",
          component: () => import("@/components/loadingtest/l1.vue"),
        },
      ],
    },
    {
      path: "/", //ok
      name: "Index",
      meta: { requiresAuth: false, role: "" },
      component: () => import("@/views"),
      children: [
        {
          path: "community", //forbid 子路由未登录都不能看，跳到登录页
          name: "Community",
          meta: { requiresAuth: true, role: "user" },
          component: () => import("@/views/maintenance-wzy/index.vue"),
          children: [
            {
              path: "parking",
              name: "parking",
              meta: { requiresAuth: true, role: "user" },
              component: () => import("@/components/service-wzy/parking.vue"),
            },
            {
              path: "repair",
              name: "repair",
              meta: { requiresAuth: true, role: "user" },
              component: () => import("@/components/service-wzy/repair.vue"),
            },
            {
              path: "visitors",
              name: "visitors",
              meta: { requiresAuth: true, role: "user" },
              component: () => import("@/views/socialservice-grc"),
            },
            {
              path: "complaints",
              name: "complaints",
              meta: { requiresAuth: true, role: "user" },
              component: () => import("@/views/function-zjm/complaints.vue"),
            },
            {
              path: "notice",
              name: "notice",
              meta: { requiresAuth: true, role: "user" },
              component: () =>
                import("@/views/socialservice-wjq/notice/index.vue"),
            },
            {
              path: "payment",
              name: "payment",
              meta: { requiresAuth: true, role: "user" },
              component: () => import("@/views/property-lzm"),
            },
          ],
        },
        {
          path: "/store", //ok 可以看但是商品点击后未登录要跳转登录界面
          name: "Store",
          component: () => import("@/views/store-grc/index.vue"),
          props: (route) => ({
            goodsID: route.query.goodsID,
            categoryId: route.query.categoryId,
          }),
        },
      ],
    },
    {
      path: "/login", //ok 用户登录
      name: "Login",
      component: () => import("@/views/login1-zjm/login.vue"),
    },
    {
      path: "/merchant-login", //OK 商户登录
      name: "merchant-Login",
      component: () => import("@/views/login1-zjm/merchant-login.vue"),
    },
    {
      path: "/trolley", //forbid 未登录跳转/login
      name: "trolley",
      props: (route) => ({ st: route.query.st }),
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/function-zjm/trolley.vue"),
    },
    {
      path: "/trolley2", //forbid 未登录跳转/login
      name: "trolley2",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/function-zjm/trolley2.vue"),
    },
    {
      path: "/personalCenter", //forbid 未登录跳转/login
      name: "PersonalCenter",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/personalCenter/index.vue"),
    },
    {
      path: "/deliveryAddress", //forbid 未登录跳转/login
      name: "DeliveryAddress",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/deliveryAddress-lzm/index.vue"),
    },
    {
      path: "/accountInformation", //forbid 未登录跳转/login
      name: "AccountInformation",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/accountInformation-lzm/index.vue"),
    },

    {
      //forbid 未登录跳转商家登录  -- /merchant-login
      path: "/shop",
      name: "Shop", //子组件未登录都不行
      meta: { requiresAuth: true, role: "merchant" },
      component: () => import("@/views/shop-grc/index.vue"),
      children: [
        {
          path: "myproduct",
          name: "myproduct",
          meta: { requiresAuth: true, role: "merchant" },
          component: () => import("@/views/shop-grc/component/myproduct"),
        },
        {
          path: "shop_apply",
          name: "shop_apply",
          meta: { requiresAuth: true, role: "merchant" },
          component: () => import("@/components/shop-wzy/applyGoods"),
        },
        {
          path: "shop_information",
          name: "shop_information",
          meta: { requiresAuth: true, role: "merchant" },
          component: () =>
            import("@/views/shop-grc/component/shop_infomation.vue"),
        },
        {
          path: "shop_money",
          name: "shop_money",
          component: () => import("@/views/shop-grc/component/shop_money.vue"),
        },
      ],
    },
    {
      path: "/merchant-completeInformation",
      name: "merchantCompleteInformation",
      component: () =>
        import("@/views/completePersonal_information-lzm/merchant.vue"),
      meta: { requiresAuth: true, role: "merchant" },
    },
    {
      //登陆之后,紧接着是完善信息
      path: "/completeInformation",
      name: "CompleteTheInformation",
      meta: { requiresAuth: true, role: "user" },
      component: () =>
        import("@/views/completePersonal_information-lzm/index.vue"),
    },
    {
      path: "/settlement", //未登录不行
      name: "MakeOrder",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/function-zjm/settlement.vue"),
    },
    {
      path: "/editAddress", //未登录不行
      name: "EditAddress",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/deliveryAddress-lzm/index.vue"),
    },
    {
      path: "/userInformation", //未登录不行
      name: "userInformation",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/completePersonal_information-lzm"),
    },
    {
      path: "/accountClosure", //forbid
      name: "AccountClosure",
      meta: { requiresAuth: true, role: "user" },
      component: () =>
        import("@/views/accountInformation-lzm/accountClosure.vue"),
    },
    {
      //ok
      path: "/contact",
      name: "Contact",
      component: () => import("@/views/contact-mmKing/index.vue"),
    },
    {
      path: "/aboutUs", //ok
      name: "AboutUs",
      component: () => import("@/views/aboutUs/index.vue"),
    },
    {
      path: "/myPurse", //forbid
      name: "MyPurse",
      meta: { requiresAuth: true, role: "user" },
      component: () => import("@/views/function-zjm/myPurser.vue"),
      children: [
        {
          path: "account",
          name: "account",
          meta: { requiresAuth: true, role: "user" },
          component: () => import("@/views/function-zjm/accoount.vue"),
        },
        {
          path: "recharge",
          name: "recharge",
          meta: { requiresAuth: true, role: "user" },
          component: () => import("@/views/function-zjm/recharge.vue"),
        },
      ],
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 };
  },
});

const isAuthenticated = (role) => {
  const Role = VueCookies.get("role");
  const token = VueCookies.get("token");
  return !!token && Role === role;
};

router.beforeEach((to, from, next) => {
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    const role = to.meta.role;
    if (!isAuthenticated(role)) {
      if (role === "user") {
        next({ name: "Login", query: { redirect: to.fullPath } });
      } else if (role === "merchant") {
        next({ name: "merchant-login", query: { redirect: to.fullPath } });
      } else {
        next({ name: "index" });
      }
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
